package com.lw.smartdevice.controller;

import com.lw.smartdevice.dto.R;
import com.lw.smartdevice.dto.UserDto;
import com.lw.smartdevice.pojo.SysUserInfo;
import com.lw.smartdevice.service.SysUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2022/3/17 9:24
 */
@RestController
@RequestMapping("/users")
public class UserController {

    private SysUserInfoService sysUserInfoService;

    @Autowired
    public void setSysUserInfoService(SysUserInfoService sysUserInfoService) {
        this.sysUserInfoService = sysUserInfoService;
    }

    @PostMapping("/registries")
    public R registry(@RequestBody UserDto userDto){
        //把UserDto转到SysUserInfo中
        SysUserInfo sysUserInfo=new SysUserInfo();
        BeanUtils.copyProperties(userDto,sysUserInfo);//拷贝对象，会将相同属性的内容复制过去
        sysUserInfoService.addUser(sysUserInfo);
        return R.setOK();
    }

    @PostMapping("/login")
    public R login(@RequestBody UserDto userDto, HttpSession session  ){
        SysUserInfo userInfo = sysUserInfoService.login(userDto);
        session.setAttribute("user",userInfo);
        return R.setOK();
    }
}
